package org.robby.hive.jdbc;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * drop table if exists tab_indb_tmp; create table tab_indb_tmp(oaddr string,oareacode string,daddr string,dareacode string ,ts string, type string) row format delimited fields terminated by ',';
 * drop table if exists tab_indb; create table tab_indb(oaddr string,oareacode string,daddr string,dareacode string ,ts string, type string) partitioned by (date string) stored as rcfile;
 * 
 * @author conkeyn
 * 
 */
public class HiveMaintanData {

	public static void main(String[] args) throws Exception {
		Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
		Connection conn = DriverManager.getConnection("jdbc:hive://hadoop.main:10000/default");
		Statement stmt = conn.createStatement();
		String dirPath = "/home/conkeyn/jar/output1/";
		File dir = new File(dirPath);
		while (true) {
			File[] fl = dir.listFiles();
			if (fl == null || fl.length == 0) {
				System.out.println("No file, sleep!");
				Thread.sleep(2000);
				continue;
			}
			for (File f : fl) {
				long start = System.currentTimeMillis();
				String sql = "load data local inpath '" + f.getAbsolutePath() + "' overwrite into table tab_indb_tmp";
				System.out.println(sql);
				ResultSet rs = stmt.executeQuery(sql);

				sql = "from tab_indb_tmp insert into table tab_indb partition(date=\"1st\") select * where substr(ts,7,2)<=\"10\" ";
				sql += "insert into table tab_indb partition(date=\"2ed\") select * where substr(ts,7,2) between \"11\" and \"20\" ";
				sql += "insert into table tab_indb partition(date=\"3rd\") select * where substr(ts,7,2)>\"20\"";
				rs = stmt.executeQuery(sql);
				f.delete();
				long end = System.currentTimeMillis();
				System.out.println("Use times:" + ((end - start) / 1000));
			}
		}

	}

}
